package com.aptemo.webetek.client.widgets.teams;

import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumUserRole;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.widgets.CurrencyWidget;
import com.aptemo.webetek.client.widgets.HorizontalMenu;
import com.aptemo.webetek.client.widgets.dialogs.DecisionDialog;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;

public class TeamHeader
		extends Composite implements TeamHeaderPresenter.Display
{

	private static TeamHeaderUiBinder	uiBinder	= GWT.create(TeamHeaderUiBinder.class);

	interface TeamHeaderUiBinder extends UiBinder<Widget, TeamHeader>
	{
	}

	@UiField
	HorizontalMenu		menus;

	@UiField
	Label				titleField;

	@UiField
	Label				roleField;

	@UiField
	Label				descriptionField;

	@UiField
	Label				infoField;

//	@UiField
//	CurrencyWidget		reinvestField;

	@UiField
	CurrencyWidget		balanceField;

	@UiField
	Hyperlink			membersLink;

	@UiField
	Hyperlink			paymentsLink;

	@UiField
	Hyperlink			betsLink;

	@UiField
	Hyperlink			editLink;

	@UiField
	Hyperlink			standingsLink;

	@UiField
	Anchor				rulesLink;
//
//	@UiField
//	Anchor				earningsLink;

	@UiField
	Anchor				ownerField;

	DecisionDialog		reinvestConfirmDialog	= new DecisionDialog("Reinvesting money...");

	private PopupPanel	popup					= new PopupPanel(true);

	public TeamHeader()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public HasText getDescField()
	{
		return descriptionField;
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasText getTitleField()
	{
		return titleField;
	}

	@Override
	public HasText getRoleField()
	{
		return roleField;
	}

	@Override
	public HasValue<Double> getPlayerBalanceField()
	{
		return balanceField;
	}

//	@Override
//	public HasValue<Double> getReinvestField()
//	{
//		return reinvestField;
//	}

	@Override
	public HasClickHandlers getMembersLink()
	{
		return membersLink;
	}

	@Override
	public HasClickHandlers getPaymentsLink()
	{
		return paymentsLink;
	}

//	@UiHandler("earningsLink")
//	void reinvestClickHandler(ClickEvent event)
//	{
//		// if (reinvestField.getValue() > 0)
//		// {
//		//
//		// reinvestConfirmDialog.setQuestionString("Are you sure you want to reinvest "
//		// + reinvestField.getText() + "?");
//		// reinvestConfirmDialog.center();
//		// }
//	}

	@Override
	public void setRules(String rules)
	{
		popup.clear();
		TextArea tb = new TextArea();
		tb.setText(rules);
		tb.setCharacterWidth(80);
		popup.add(tb);
	}

	@UiHandler("rulesLink")
	void rulesClickHandler(ClickEvent event)
	{
		Widget source = (Widget) event.getSource();
		int left = source.getAbsoluteLeft() + 10;
		int top = source.getAbsoluteTop() + 10;

		popup.setPopupPosition(left, top);
		popup.show();
	}

	@Override
	public HasDecisionHandlers getReinvestConfirmDialog()
	{
		return reinvestConfirmDialog;
	}

	@Override
	public void setHyperlinkTargets(String teamId, String ownerEmail)
	{
		membersLink.setTargetHistoryToken("members-" + teamId);
		paymentsLink.setTargetHistoryToken("payments-" + teamId);
		betsLink.setTargetHistoryToken("group-" + teamId);
		editLink.setTargetHistoryToken("groupedit-" + teamId);
		standingsLink.setTargetHistoryToken("standings-" + teamId);
		ownerField.setHref("mailto:" + ownerEmail);
	}

	@Override
	public void setMode(EnumUserRole role)
	{
		menus.setVisible(true);
		switch (role)
		{
			case EDITOR:
			case ADMIN:
				menus.setMenuItemVisible(true, membersLink);
				menus.setMenuItemVisible(true, editLink);
				menus.setMenuItemVisible(true, paymentsLink);
				menus.setMenuItemVisible(true, standingsLink);
				menus.setMenuItemVisible(true, rulesLink);
				//earningsLink.setVisible(true);
				ownerField.setVisible(true);
				break;
			case GAMER:
				menus.setMenuItemVisible(false, membersLink);
				menus.setMenuItemVisible(false, editLink);
				menus.setMenuItemVisible(true, standingsLink);
				menus.setMenuItemVisible(true, paymentsLink);
				menus.setMenuItemVisible(true, rulesLink);
				//earningsLink.setVisible(true);
				ownerField.setVisible(true);
				break;
			default:
				menus.setMenuItemVisible(false, betsLink);
				menus.setMenuItemVisible(false, editLink);
				menus.setMenuItemVisible(false, membersLink);
				menus.setMenuItemVisible(false, paymentsLink);
				menus.setMenuItemVisible(false, standingsLink);
				menus.setMenuItemVisible(false, rulesLink);
				ownerField.setText("...");
				ownerField.setHref("");

				break;
		}
		ownerField.setText("...");
		ownerField.setHref("");
	}

	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

	@Override
	public HasText getOwnerField()
	{
		return ownerField;
	}

}
